package evidence.view;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Calendar;

/**
 *
 * @author Protheo
 */
public class NewMonth extends javax.swing.JDialog {
    public static final long serialVersionUID = 1;
    private Integer mesic, rok;
    public static final int RET_OK = 0;
    public static final int RET_CANCEL = 1;
    private int retState = 1;

    /** Creates new form NewJDialog
     * @param parent
     * @param modal
     */
    public NewMonth(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        mesic = Calendar.getInstance().get(Calendar.MONTH)+1;
        rok = Calendar.getInstance().get(Calendar.YEAR);
        initComponents();
        this.addWindowListener(new MyWindowAdapter((View) parent));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        sRok = new javax.swing.JSpinner();
        jLabel2 = new javax.swing.JLabel();
        ok = new javax.swing.JButton();
        cancel = new javax.swing.JButton();
        sMesic = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Měsíc:");

        sRok.setModel(new javax.swing.SpinnerNumberModel(2011, 2011, 2020, 1));
        sRok.setEditor(new javax.swing.JSpinner.NumberEditor(sRok, "####"));
        sRok.setValue(this.rok);

        jLabel2.setText("Rok:");

        ok.setText("OK");
        ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okActionPerformed(evt);
            }
        });

        cancel.setText("Cancel");
        cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });

        sMesic.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Leden", "Únor", "Březen", "Duben", "Květen", "Červen", "Červenec", "Srpen", "Září", "Říjen", "Listopad", "Prosinec" }));
        sMesic.setSelectedIndex(this.mesic-1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(sMesic, 0, 74, Short.MAX_VALUE)
                            .addComponent(sRok, javax.swing.GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(ok)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cancel)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(sMesic, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(sRok, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ok)
                    .addComponent(cancel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okActionPerformed
        this.retState = RET_OK;
        this.mesic = this.sMesic.getSelectedIndex()+1;
        this.rok = (Integer) this.sRok.getValue();
        this.processWindowEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
    }//GEN-LAST:event_okActionPerformed

    private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
        this.retState = RET_CANCEL;
        this.processWindowEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
    }//GEN-LAST:event_cancelActionPerformed

    public int getMesic(){
        return mesic;
    }

    public int getRok(){
        return rok;
    }

    public int getRetState() {
        return retState;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JButton ok;
    private javax.swing.JComboBox sMesic;
    private javax.swing.JSpinner sRok;
    // End of variables declaration//GEN-END:variables

    private class MyWindowAdapter extends WindowAdapter {

        private View okno = null;

        public MyWindowAdapter(View okno) {
            this.okno = okno;
        }

        @Override
        public void windowClosing(WindowEvent e) {
            NewMonth dotaz = (NewMonth) e.getWindow();
            if (dotaz.getRetState() == NewMonth.RET_OK) {
                okno.newMonth(dotaz.getRok(), dotaz.getMesic());
            }
            super.windowClosing(e);
        }
    }
}
